package cn.lvhang.pay.service.impl;

import cn.lvhang.basic.exception.BusinessException;
import cn.lvhang.basic.service.impl.BaseServiceImpl;
import cn.lvhang.order.service.IProductOrderService;
import cn.lvhang.pay.domain.AlipayInfo;
import cn.lvhang.pay.domain.PayBill;
import cn.lvhang.pay.mapper.AlipayInfoMapper;
import cn.lvhang.pay.mapper.PayBillMapper;
import cn.lvhang.pay.service.IPayBillService;
import cn.lvhang.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill billTmp) {
        //校验
        if(billTmp == null){
            throw new BusinessException("支付数据为空！");
        }
        PayBill bill = payBillMapper.loadById(billTmp.getId());
        if(bill == null){
            throw new BusinessException("支付数据为空！");
        }
        //区分支付类型  //0 余额 1 支付宝 2 微信 3 银联
        switch (bill.getPayChannel().intValue()){
            case 1 ://支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(info, bill);
            case 2 ://微信
                //语句
                break; //可选
            case 3 ://银联
                //语句
                break; //可选
            default : //余额
                //语句
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }


}
